package com.lgx.mobile.rest.business;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.mobile.model.OrganizationModel;
import com.lgx.right.entity.Organization;
import com.lgx.right.service.IOrganizationService;

/**
 * 
 * @author xuyichen
 *
 */
@Controller
@RequestMapping("/getOrganizationList")
public class GetOrganizationListController {
	
	private static final Logger logger = Logger.getLogger(GetOrganizationListController.class);

	@Autowired
	IOrganizationService organizationService;

	public IOrganizationService getOrganizationService() {
		return organizationService;
	}

	/**
	 * 商家查询
	 * @param pointsX pointsX is key
	 * @param pointsY pointsY is key
	 * @return 距离1000米以内所有商家信息集合；0：商家列表为空；1：参数异常，有参数为空；2：坐标参数格式错误，只能是数字
	 */
	@RequestMapping(value="/{pointsX}/{pointsY}",method=RequestMethod.GET)
	@ResponseBody
	public Object getOrganizationList(@PathVariable("pointsX") String pointsX,@PathVariable("pointsY") String pointsY){
		if(StringUtils.isNotBlank(pointsX)&&StringUtils.isNotBlank(pointsY)){
			List<Organization> organizationList = new ArrayList<Organization>();
			try{
				organizationList = getOrganizationService().findOrganizationListByPoints(Double.parseDouble(pointsX), Double.parseDouble(pointsY));
			}catch(NumberFormatException nume){
				return "2";
			}
			if(organizationList.size() != 0){
				List<OrganizationModel> organizationModelList = new ArrayList<OrganizationModel>();
				for(int i=0;i<organizationList.size();i++){
					OrganizationModel organizationModel = new OrganizationModel(null);
					organizationModel.setId(organizationList.get(i).getId());
					organizationModel.setName(organizationList.get(i).getName());
					organizationModel.setLogoUrl(organizationList.get(i).getLogoUrl());
					organizationModel.setDescription(organizationList.get(i).getDescription());
					organizationModel.setPoints(organizationList.get(i).getPoints());
					organizationModel.setStar(organizationList.get(i).getStar());
					organizationModel.setAddress(organizationList.get(i).getAddress());
					organizationModel.setTel(organizationList.get(i).getTel());
					organizationModelList.add(organizationModel);
				}
				return organizationModelList;
			}else{
				if(logger.isDebugEnabled()){
					logger.debug("getOrganizationList faild pointsX and pointsY is error");
				}
//				"getOrganizationList faild organizationList is no record";
				return "0";
			}
		}else{
			if(logger.isDebugEnabled()){
				logger.debug("getOrganizationList faild pointsX and pointsY is not filled");
			}
//		    "getOrganizationList faild please check pointsX and pointsY is filled";
			return "1";
		}
	}
}
